import path from 'path'

const { remote } = require('electron')
const fs = require('fs')
const crypto = require('crypto')
const storage = require('electron-json-storage')
const moment = require('moment')
export default {
    /**
     * 选择目录
     * @returns {Promise<unknown>}
     */
    selectDir() {
        return new Promise((resolve, reject) => {
            remote.dialog.showOpenDialog({ properties: ['openDirectory', 'openFile'] }).then(res => {
                if (!res.canceled) {
                    resolve(res.filePaths[0])
                } else {
                    reject()
                }
            })
        })
    },
    /**
     * 获得文件hash值
     * @param file
     * @returns {string|boolean}
     */
    hash(file) {
        if (fs.existsSync(file) && fs.statSync(file).isFile()) {
            const buffer = fs.readFileSync(file)
            const fsHash = crypto.createHash('md5')
            fsHash.update(buffer)
            return fsHash.digest('hex')
        } else {
            return false
        }
    },
    /**
     * 扫描目录文件
     * @param dirPath
     * @param child
     * @returns {object}
     */
    async scanDir(dirPath, child = false) {
        const dir = await fs.promises.opendir(dirPath)
        let files = []
        let dirs = []
        for await (const d of dir) {
            let filePath = dirPath + '/' + d.name
            console.log(d)
            let fileInfo = fs.statSync(filePath)

            let time = fileInfo.mtimeMs
            time = moment(time).format('YYYY-MM-DD hh:mm:ss')
            let size = fs.statSync(filePath).size
            let kb = 1024
            let mb = kb * 1024
            let gb = mb * 1024
            if (size > gb) {
                size = (size / gb).toFixed(2) + 'GB'
            } else if(size > mb) {
                size = (size / mb).toFixed(2) + 'MB'
            } else {
                size = (size / kb).toFixed(2) + 'kb'
            }
            let isDir = fileInfo.isDirectory()
            let info = {
                path: filePath,
                name: d.name,
                time: time,
                ext: path.extname(filePath).substr(1),
                size: isDir ? '' : size,
                isDir: isDir
            }
            if (fileInfo.isDirectory()) {  // 目录

                dirs.push(info)
                // 循环遍历
                if (child) {
                    let childFile = await this.scanDir(filePath, true)
                    files = files.concat(childFile.files)
                    dirs = dirs.concat(childFile.dirs)
                }
            } else {
                files.push(info)
            }
        }
        return { files, dirs }
    },
    pathToKey(path) {
        return path.replace(/[\/\\:]/g, '-').replace('--','-')
    },
    // 缓存文件路径
    setCache(path, files) {
        storage.set(this.pathToKey(path), files, () => {
            //
        })
    },
    // 获取缓存的文件路径
    getCache(path) {
        return new Promise((resolve, reject) => {
            storage.get(this.pathToKey(path), {}, (err, data) => {
                if(err) {
                    reject()
                } else {
                    resolve(data)
                }
            })
        })
    },
    createCacheFile(path) {

    },
    // 记录文件备注
    logFileNote(file, note) {

    }
}
